package com.sduept.nwld.dataserver.controller.base;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.fault.special.maintenance.manager.MaintenanceLogManager;
import com.sduept.bigdata.fault.special.maintenance.manager.MaintenanceManager;
import com.sduept.bigdata.special.maintenance.model.MaintenanceLog;
import com.sduept.bigdata.special.maintenance.model.MaintenanceStation;
import com.sduept.bigdata.web.jsf.controller.AbstractController;

/**
 * @author Administrator 保护运行-特维厂站信息（仅有查询功能）
 */
@Named
@ViewScoped
public class SpecialMaintenanceStationQueryController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4244044488472367032L;
	@Autowired
	private MaintenanceManager sm;
	@Autowired
	private MaintenanceLogManager logM;

	private List<MaintenanceStation> unBindStations = new ArrayList<MaintenanceStation>();// 未关联设备

	private List<MaintenanceStation> maintenanceStations = new ArrayList<MaintenanceStation>(); // 特维设备
	public Map<String, List<String>> specialStationMap = new HashMap<>();

	/**
	 * 查询条件
	 */
	private MaintenanceStation queryMS = new MaintenanceStation();
	private String volQuery = "500";
	private String cutPowerPlanQuery = null; // 是否安排了停电计划
	private String finishBeforeQuery = null; // 是否6.30之前应该完成
	private String finishQuery = null; // 6.30之前是否已经完成
	private Date chuandongTimeStart; // 传动时间
	private Date chuandongTimeEnd; // 传动时间
	private Date finishedTimeStart; // 完成时间
	private Date finishedTimeEnd; // 完成时间
	private List<MaintenanceLog> maintenanceLogs;
	private List<String> maintenanceLogInfos;

	@PostConstruct
	private void init() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		queryMS.setYear(year);
		query();
	}

	public void changeYear() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		String year = map.get("year").toString();
		int y = Integer.valueOf(year);
		queryMS.setYear(y);
	}

	/**
	 * 特维厂站查询列表
	 * 
	 * @param query
	 * @return
	 */
	public List<String> completeSpStation(String query) {
		List<String> results = new ArrayList<String>();
		for (String s : specialStationMap.keySet()) {
			if (s.startsWith(query)) {
				results.addAll(specialStationMap.get(s));
			}
		}
		return results;
	}

	/**
	 * 所有未绑定设备的纪录
	 */
	public void getUnBind() {
		unBindStations = sm.getUnBindStations(queryMS.getYear());
	}

	public void query() {
		queryMS.setVoltage(Integer.parseInt(volQuery));
		maintenanceStations = sm.getMaintenanceStationByCondition(queryMS, cutPowerPlanQuery, finishBeforeQuery,
				finishQuery, chuandongTimeStart, chuandongTimeEnd, finishedTimeStart, finishedTimeEnd);
	}

	public void resetQuery() {
		queryMS = new MaintenanceStation();
		volQuery = "500";
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		queryMS.setYear(year);
		cutPowerPlanQuery = null;
		finishBeforeQuery = null;
		finishQuery = null;
		chuandongTimeStart = null;
		chuandongTimeEnd = null;
		finishedTimeStart = null;
		finishedTimeEnd = null;
		query();
	}

	public void showMaintenanceLog(MaintenanceStation maintenanceStation) {
		if (maintenanceLogInfos != null) {
			maintenanceLogInfos.clear();
		} else {
			maintenanceLogInfos = new ArrayList<String>();
		}
		List<MaintenanceLog> temp = logM.findMaintenanceLogByDeviceIdStationIdTypeAndYear(maintenanceStation.getBlankId(),maintenanceStation.getSubStationId(),
				"chuandong", maintenanceStation.getYear());
		for (MaintenanceLog m : temp) {
			String log = m.getInfo();
			maintenanceLogInfos.add(log);
		}
	}

	public class maintenanceLogInfo {
		private String phase;
		private String faultType;
		private String time;

		public String getPhase() {
			return phase;
		}

		public void setPhase(String phase) {
			this.phase = phase;
		}

		public String getFaultType() {
			return faultType;
		}

		public void setFaultType(String faultType) {
			this.faultType = faultType;
		}

		public String getTime() {
			return time;
		}

		public void setTime(String time) {
			this.time = time;
		}
	}

	public List<MaintenanceStation> getUnBindStations() {
		return unBindStations;
	}

	public void setUnBindStations(List<MaintenanceStation> unBindStations) {
		this.unBindStations = unBindStations;
	}

	public List<MaintenanceStation> getMaintenanceStations() {
		return maintenanceStations;
	}

	public void setMaintenanceStations(List<MaintenanceStation> maintenanceStations) {
		this.maintenanceStations = maintenanceStations;
	}

	public String getVolQuery() {
		return volQuery;
	}

	public void setVolQuery(String volQuery) {
		this.volQuery = volQuery;
	}

	public MaintenanceStation getQueryMS() {
		return queryMS;
	}

	public void setQueryMS(MaintenanceStation queryMS) {
		this.queryMS = queryMS;
	}

	public String getCutPowerPlanQuery() {
		return cutPowerPlanQuery;
	}

	public void setCutPowerPlanQuery(String cutPowerPlanQuery) {
		this.cutPowerPlanQuery = cutPowerPlanQuery;
	}

	public String getFinishBeforeQuery() {
		return finishBeforeQuery;
	}

	public void setFinishBeforeQuery(String finishBeforeQuery) {
		this.finishBeforeQuery = finishBeforeQuery;
	}

	public String getFinishQuery() {
		return finishQuery;
	}

	public void setFinishQuery(String finishQuery) {
		this.finishQuery = finishQuery;
	}

	public Date getChuandongTimeStart() {
		return chuandongTimeStart;
	}

	public void setChuandongTimeStart(Date chuandongTimeStart) {
		this.chuandongTimeStart = chuandongTimeStart;
	}

	public Date getChuandongTimeEnd() {
		return chuandongTimeEnd;
	}

	public void setChuandongTimeEnd(Date chuandongTimeEnd) {
		this.chuandongTimeEnd = chuandongTimeEnd;
	}

	public Date getFinishedTimeStart() {
		return finishedTimeStart;
	}

	public void setFinishedTimeStart(Date finishedTimeStart) {
		this.finishedTimeStart = finishedTimeStart;
	}

	public Date getFinishedTimeEnd() {
		return finishedTimeEnd;
	}

	public void setFinishedTimeEnd(Date finishedTimeEnd) {
		this.finishedTimeEnd = finishedTimeEnd;
	}

	public List<MaintenanceLog> getMaintenanceLogs() {
		return maintenanceLogs;
	}

	public void setMaintenanceLogs(List<MaintenanceLog> maintenanceLogs) {
		this.maintenanceLogs = maintenanceLogs;
	}

	public List<String> getMaintenanceLogInfos() {
		return maintenanceLogInfos;
	}

	public void setMaintenanceLogInfos(List<String> maintenanceLogInfos) {
		this.maintenanceLogInfos = maintenanceLogInfos;
	}

}
